// date.js
export function formatDate(date, fmt) {
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	}
	let o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	};
	for (let k in o) {
		if (new RegExp(`(${k})`).test(fmt)) {
			let str = o[k] + '';
			fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : padLeftZero(str));
		}
	}
	return fmt;
}

function padLeftZero(str) {
	return ('00' + str).substr(str.length);
}

export function str2Date(dateStr, separator) {
	if (!separator) {
		separator = "-";
	}
	let dateArr = dateStr.split(separator);
	let year = parseInt(dateArr[0]);
	let month;
	//处理月份为04这样的情况
	if (dateArr[1].indexOf("0") == 0) {
		month = parseInt(dateArr[1].substring(1));
	} else {
		month = parseInt(dateArr[1]);
	}
	let day = parseInt(dateArr[2]);
	let date = new Date(year, month - 1, day);
	return date;
}

var now = new Date(); //当前日期 
var nowDayOfWeek = now.getDay(); //今天本周的第几天 
var nowDay = now.getDate(); //当前日 
var nowMonth = now.getMonth(); //当前月 
var nowYear = now.getYear(); //当前年 
nowYear += (nowYear < 2000) ? 1900 : 0; //

function formatDateStr(date) {
	var myyear = date.getFullYear();
	var mymonth = date.getMonth() + 1;
	var myweekday = date.getDate();

	if (mymonth < 10) {
		mymonth = "0" + mymonth;
	}
	if (myweekday < 10) {
		myweekday = "0" + myweekday;
	}
	return (myyear + "-" + mymonth + "-" + myweekday);
}

function getMonthDays(myMonth) {
	var monthStartDate = new Date(nowYear, myMonth, 1);
	var monthEndDate = new Date(nowYear, myMonth + 1, 1);
	var days = (monthEndDate - monthStartDate) / (1000 * 60 * 60 * 24);
	return days;
}

//获得本周的开始日期 
export function getWeekStartDate() {
	var weekStartDate = new Date(nowYear, nowMonth, nowDay - nowDayOfWeek);
	return formatDateStr(weekStartDate);
}

//获得本周的结束日期 
export function getWeekEndDate() {
	var weekEndDate = new Date(nowYear, nowMonth, nowDay + (6 - nowDayOfWeek));
	return formatDateStr(weekEndDate);
}

//获得本月的开始日期 
export function getMonthStartDate() {
	var monthStartDate = new Date(nowYear, nowMonth, 1);
	return formatDateStr(monthStartDate);
}

//获得本月的结束日期 
export function getMonthEndDate() {
	var monthEndDate = new Date(nowYear, nowMonth, getMonthDays(nowMonth));
	return formatDateStr(monthEndDate);
}

export function getTimeDiff(time1, time2) {
	//定义两个变量time1,time2分别保存开始和结束时间
	// var time1 = "2017-12-02 12:25";
	// var time2 = "2017-12-03 12:35";
	//判断开始时间是否大于结束日期
	if (time1 > time2) {
		alert("开始时间不能大于结束时间！");
		return false;
	}
	//截取字符串，得到日期部分"2009-12-02",用split把字符串分隔成数组
	var begin1 = time1.substr(0, 10).split("-");
	var end1 = time2.substr(0, 10).split("-");
	//将拆分的数组重新组合，并实例成化新的日期对象
	var date1 = new Date(begin1[1] + - +begin1[2] + - +begin1[0]);
	var date2 = new Date(end1[1] + - +end1[2] + - +end1[0]);
	//得到两个日期之间的差值m，以分钟为单位
	//Math.abs(date2-date1)计算出以毫秒为单位的差值
	//Math.abs(date2-date1)/1000得到以秒为单位的差值
	//Math.abs(date2-date1)/1000/60得到以分钟为单位的差值
	var m = parseInt(Math.abs(date2 - date1) / 1000 / 60);
	//小时数和分钟数相加得到总的分钟数
	//time1.substr(11,2)截取字符串得到时间的小时数
	//parseInt(time1.substr(11,2))*60把小时数转化成为分钟
	var min1 = parseInt(time1.substr(11, 2)) * 60 + parseInt(time1.substr(14, 2));
	var min2 = parseInt(time2.substr(11, 2)) * 60 + parseInt(time2.substr(14, 2));
	//两个分钟数相减得到时间部分的差值，以分钟为单位
	var n = min2 - min1;
	//将日期和时间两个部分计算出来的差值相加，即得到两个时间相减后的分钟数
	var minutes = m + n;
	return minutes;
}
